import numpy as np
from scipy import stats
import pandas as pd

excel_path = "Zheng_Tai_Fen_Bu.xlsx"
df = pd.read_excel(excel_path, sheet_name="Sheet1", usecols="A", header=None)

# 将DataFrame转换为NumPy数组
data = df[0].values  # 替换为实际列名

# Shapiro-Wilk 正态性检验
shapiro_stat, shapiro_p_value = stats.shapiro(data)
print(f"Shapiro-Wilk Test: Statistic={shapiro_stat}, p-value={shapiro_p_value}")

# Kolmogorov-Smirnov 正态性检验（与标准正态分布比较）
ks_stat, ks_p_value = stats.kstest(data, 'norm')
print(f"Kolmogorov-Smirnov Test: Statistic={ks_stat}, p-value={ks_p_value}")

# Anderson-Darling 正态性检验
anderson_result = stats.anderson(data, dist='norm')
print(f"Anderson-Darling Test: Statistic={anderson_result.statistic}, "
      f"Critical Values={anderson_result.critical_values}, "
      f"Significance Levels={anderson_result.significance_level}")

# 解释p值
alpha = 0.05  # 显著性水平为5%
if shapiro_p_value < alpha:
    print("Shapiro-Wilk Test: 拒绝原假设，数据不符合正态分布。")
else:
    print("Shapiro-Wilk Test: 无法拒绝原假设，数据可能符合正态分布。")

if ks_p_value < alpha:
    print("Kolmogorov-Smirnov Test: 拒绝原假设，数据不符合正态分布。")
else:
    print("Kolmogorov-Smirnov Test: 无法拒绝原假设，数据可能符合正态分布。")

# Anderson-Darling 测试的解释稍微复杂一些，因为它提供了多个临界值
# 和显著性水平，你需要检查统计量是否大于任何给定的临界值
for i, (cv, sl) in enumerate(zip(anderson_result.critical_values, anderson_result.significance_level)):
    if anderson_result.statistic > cv:
        print(f"Anderson-Darling Test: 在显著性水平 {sl:.2f} 下，拒绝原假设，数据不符合正态分布。")
        break
else:
    print("Anderson-Darling Test: 无法拒绝原假设，数据可能符合正态分布。")






